<template>
	<div class="page">
		<!-- 面包屑 -->
		<el-breadcrumb separator-class="el-icon-arrow-right" class="admin-breadcrumb">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item :to="{ path: '/user' }">用户管理</el-breadcrumb-item>
			<el-breadcrumb-item>{{title}}</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- form -->
		<div class="headings">{{title}}</div>
		<div class="admin-form">
			<el-form ref="form" :model="form" label-width="80px">
				<el-form-item label="平台名称" prop="Nickname" :rules="[{ required: true, message: '平台名称不能为空', trigger: 'blur' }]">
					<el-input v-model="form.Nickname"></el-input>
				</el-form-item>
        <el-form-item label="邮箱地址" prop="UserEmail" :rules="[{ required: true, type: 'email', message: '邮箱地址不能为空', trigger: 'blur' }]">
					<el-input v-model="form.UserEmail"></el-input>
				</el-form-item>
        <el-form-item label="电话" prop="Tel" :rules="[{ required: true, message: '电话不能为空', trigger: 'blur' }]">
					<el-input v-model="form.Tel"></el-input>
				</el-form-item>
        <div v-if="form.UserID==null">
        <el-form-item label="密码" prop="UserPwd" :rules="[{ required: true, message: '密码不能为空', trigger: 'blur' }]">
					<el-input v-model="form.UserPwd"></el-input>
				</el-form-item>
        </div>
				<el-form-item>
					<el-button type="primary" @click="onSubmit" :loading="postLoading">提交</el-button>
					<el-button onclick="history.go(-1)">返回</el-button>
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>

<script>
export default {
  data() {
    return {
      title: this.$route.name,
      loading: false,
      postLoading: false,
      form: {
        Nickname: "",
        Tel: "",
        UserEmail: "",
        UserPwd:null,
        UserID:null,
      }
    };
  },
  mounted: function() {
    // 判断编辑
    if (this.$route.query.id) {
      this.form.UserID = this.$route.query.id;
      this.getData(this.$route.query.id);
    }
  },
  methods: {
    // 提交信息
    onSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.postLoading = true;
          this.$http.post("/User/AddEdit", this.form).then(response => {
              if (response.code == 0) {
                this.$message({
                  message: "提交成功，返回列表",
                  type: "success",
                  duration: 2000,
                  onClose: function() {
                    window.history.go(-1);
                  }
                });
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 请求原信息
    getData(id) {
      this.$http.get("/User/AddEdit", { params: { UserID: id } }).then(response => {
          if (response.code == 0) {
          this.form = response.data;
          this.loading = false;
          }
        });
    }
  }
};
</script>

<style>
</style>